var app=new Vue({
    el: '#app',
    data:{
        userList: null,
        searchUser:{
            total:0,
            pageSize:10,
            pageIndex:1,
            startEndDate: ""
        },
        dicts:{
            depts: [],
            status: []
        }
    },
    methods:{
        init: function(){
            var _this=this;
            setTimeout(function(){
                _this.findAll();
                _this.initDict();
            },100);
        },
        exportExcel: function(){
            window.location.href=jsmile.url+"/sys/user/export?"+$.param(this.searchUser);
        },
        updateStatus: function(row,value){
            var _this=this;
            jsmile.http.sendJson({
                url:jsmile.url+"/sys/user/update",
                data:{"id":row.id,"enable":value}
            }).done(function(res){
                jsmile.msg.success("启用成功!");
                _this.query();
            }).fail(function(res){
                jsmile.msg.error(res.resMsg?res.resMsg:"启用失败!");
            });
        },
        addUser: function(){
            top.layer.open(jsmile.dialog.init({
                title: "新增账号",
                content: "sys/user/add.html"
            }));
        },
        editUser: function(index,row){
            var _this=this;
            top.layer.open(jsmile.dialog.init({
                title: "修改账号信息",
                content: "sys/user/edit.html",
                success: function(layero,index){
                    var iframeW=layero.find('iframe')[0];
                    row.deptId=jsmile.tree.getTreeData(row.deptId,_this.dicts.depts);
                    iframeW.contentWindow.app.user=row;
                }
            }));
        },
        deleteUser: function(index,row){
            var _this=this;
            top.app.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function() {
                jsmile.http.sendJson({
                    url:jsmile.url+"/sys/user/delete",
                    data:{"id":row.id}
                }).done(function(res){
                    jsmile.msg.success("删除用户成功!");
                    _this.query();
                }).fail(function(res){
                    jsmile.msg.error(res.resMsg?res.resMsg:"删除用户失败!");
                });
            }).catch(function() {
                jsmile.msg.info( '已取消删除');
            });
        },
        query: function(){
            this.searchUser.pageIndex=1;
            this.findAll();
        },
        reset: function(){
            this.searchUser={
                startEndDate: ""
            }
        },
        /**
         * 初始化数据字典
         */
        initDict: function(){
            var _this=this;
            jsmile.http.sendJson({
                url:jsmile.url+"/sys/dept/findAll",
                data:_this.searchUser
            }).done(function(res){
                var data=jsmile.tree.toTree(res.resData.list,{"textField":"deptName"});
                data.unshift({label:"全部部门",value:"-1"});
                _this.dicts.depts=data;
            });
            var s=jsmile.copy([],jsmile.dict.zt);
            s.unshift({dictName:"全部状态",dictValue:null});
            _this.dicts.status=s;
        },
        goHome: function(){
            top.$('#home').attr("src","homePage/home.html");
        },
        handleSizeChange: function(val) {
            this.searchUser.pageSize=val;
            this.findAll();
        },
        handleCurrentChange: function(val) {
            this.searchUser.pageIndex=val;
            this.findAll();
        },
        /*
         * 获取所有数据
         */
        findAll: function(){
            this.searchUser.isPage=1;
            if(!this.searchUser.startEndDate){
                this.searchUser.startEndDate="";
            }else{
                this.searchUser.startTime=this.searchUser.startEndDate[0];
                this.searchUser.endTime=this.searchUser.startEndDate[1];
            }

            var _this=this;
            jsmile.http.sendJson({
                url:jsmile.url+"/sys/user/findAll",
                data:_this.searchUser
            }).done(function(res){
                _this.userList=res.resData.list;
                _this.searchUser.total=res.resData.total;
            }).fail(function(res){
                jsmile.msg.error(res.resMsg?res.resMsg:"获取用户列表失败!");
            });
        }
    }
});
$(function(){
    init();
    function init(){
        app.init();
    }
});